﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  对象扩展方法类型。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mozlite.Common;
    using System.Web.Routing;
    using Mozlite.Common.Configuration;
    using System.Linq.Expressions;
    using Mozlite.Web.UI;
    using Mozlite.Web;

    /// <summary>
    /// 对象辅助类。
    /// </summary>
    public static class ObjectUitlity
    {
        /// <summary>
        /// 获取页面扩展名。
        /// </summary>
        /// <param name="mode">页面显示模式。</param>
        /// <returns>返回扩展名。</returns>
        public static string ToExtension(this PageMode mode)
        {
            if (mode == PageMode.NoEnding)
                return null;
            return "." + mode.ToString();
        }

        /// <summary>
        /// 将日期格式化为网站设置的日期格式字符串。
        /// </summary>
        /// <param name="datetime">当前日期实例。</param>
        /// <returns>返回格式化后的字符串。</returns>
        public static string ToLocalDateString(this DateTime datetime)
        {
            return datetime.ToString(SiteSettings.Current.DateFormat);
        }

        /// <summary>
        /// 将日期格式化为网站设置的时间格式字符串。
        /// </summary>
        /// <param name="datetime">当前日期实例。</param>
        /// <returns>返回格式化后的字符串。</returns>
        public static string ToLocalTimeString(this DateTime datetime)
        {
            return datetime.ToString(SiteSettings.Current.TimeFormat);
        }

        /// <summary>
        /// 将日期格式化为网站设置的日期和时间格式字符串。
        /// </summary>
        /// <param name="datetime">当前日期实例。</param>
        /// <returns>返回格式化后的字符串。</returns>
        public static string ToLocalDateTimeString(this DateTime datetime)
        {
            return datetime.ToString(SiteSettings.Current.DateTimeFormat);
        }

        /// <summary>
        /// 判断当前控制器上下文是否为区域上下文。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回结果。</returns>
        public static bool IsAreaContext(this ControllerContext context)
        {
            return context.RouteData.DataTokens.ContainsKey("area");
        }

        /// <summary>
        /// 获取区域名称。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回结果。</returns>
        public static string GetAreaName(this ControllerContext context)
        {
            return context.RouteData.DataTokens["area"] as string;
        }

        /// <summary>
        /// 获取应用程序类型。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回结果。</returns>
        public static ApplicationType GetApplication(this ControllerContext context)
        {
            return context.RouteData.Get<ApplicationType>("applicationtype", ApplicationType.Common);
        }

        /// <summary>
        /// 获取控制器。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回结果。</returns>
        public static string GetController(this ControllerContext context)
        {
            return context.RouteData.Get<string>("controller");
        }

        /// <summary>
        /// 获取执行方法。
        /// </summary>
        /// <param name="context">控制器上下文。</param>
        /// <returns>返回结果。</returns>
        public static string GetAction(this ControllerContext context)
        {
            return context.RouteData.Get<string>("action");
        }

        /// <summary>
        /// 获取表达式的值。
        /// </summary>
        /// <typeparam name="TModel">模型。</typeparam>
        /// <typeparam name="TDelegate">代理方法。</typeparam>
        /// <param name="helper">辅助方法。</param>
        /// <param name="expression">表达式。</param>
        /// <returns>返回执行结果。</returns>
        public static object Execute<TModel, TDelegate>(this HtmlHelper<TModel> helper, Expression<TDelegate> expression)
        {
            return helper.ViewData.Eval(ExpressionHelper.GetExpressionText(expression));
        }

        /// <summary>
        /// 将集合序列化成JSON格式。
        /// </summary>
        /// <param name="dic">集合实例对象。</param>
        /// <returns>返回JSON字符串。</returns>
        public static string ToJSON(this RouteValueDictionary dic)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in dic)
            {
                if (item.Value == null)
                    continue;
                if (sb.Length > 0)
                    sb.Append(", ");
                sb.AppendFormat("{0}:'{1}'", item.Key, item.Value.ToString().JSEncode());
            }
            return "{" + sb.ToString() + "}";
        }

        /// <summary>
        /// 时间长短。
        /// </summary>
        /// <param name="span">时间间隔。</param>
        /// <returns>返回时间字符串。</returns>
        public static string ToSpanString(this TimeSpan span)
        {
            if (span.TotalDays > 1.0)
                return (int)span.TotalDays + Resources.GetString(TimeUnit.Day);
            else if (span.TotalHours > 1.0)
                return (int)span.TotalHours + Resources.GetString(TimeUnit.Hour);
            else if (span.TotalMinutes > 1.0)
                return (int)span.TotalMinutes + Resources.GetString(TimeUnit.Minute);
            return (int)span.TotalSeconds + Resources.GetString(TimeUnit.Second);
        }

        /// <summary>
        /// 获取系统路径。
        /// </summary>
        /// <param name="directory">文档目录。</param>
        /// <returns>返回系统路径。</returns>
        public static string ToPath(this SystemDirectory directory)
        {
            switch (directory)
            { 
                case SystemDirectory.Shared:
                    return "Views/Shared";
                case SystemDirectory.Utility:
                    return "~/_utilities";
            }
            return directory.ToString();
        }
    }
}
